//
//  YZNoteAudioDisplayBtn.m
//  VET
//
//  Created by 刘文靖 on 2018/7/25.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZNoteAudioDisplayBtn.h"
#import "YZRecordManager.h"
#import "NSDate+Category.h"
#import "HcdAudioPlayer.h"
@interface YZNoteAudioDisplayBtn()<HcdAudioPlayerDelegate>
@property (nonatomic, assign) YZNoteAudioDisplayType displayType;
@property (nonatomic, strong) UIImageView *playImageView;
@property (nonatomic, strong) UILabel *countdownLabel;
@property (nonatomic, weak) NSTimer *countdownTimer;
@property (nonatomic, assign) NSInteger countdownInterval;
@end


@implementation YZNoteAudioDisplayBtn

- (instancetype)initWithDisplayType:(YZNoteAudioDisplayType )displayType audioUrl:(NSString *)audioUrl{
    self = [super init];
    if (self) {
        _displayType = displayType;
        _audioUrl = audioUrl;
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    [HcdAudioPlayer sharedInstance].delegate = self;
    self.backgroundColor = [UIColor whiteColor];
    self.layer.borderColor = kColorWithHexAlpha(0xD9D9D9, 0.5).CGColor;
    self.layer.borderWidth = 1;
    self.playImageView = [[UIImageView alloc] init];
    [self.playImageView setImage:[UIImage imageNamed:@"img_wave_play"]];
    [self.playImageView setHighlightedImage:[UIImage imageNamed:@"img_wave_stop"]];
    [self addSubview:self.playImageView];
    UIImageView *waveImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"img_wave"]];
    [self addSubview:waveImageView];
    self.countdownLabel = [[UILabel alloc] init];
    self.countdownLabel.textColor = kColorWithHex(0x32BE60);
    self.countdownLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:16];
    [self addSubview:self.countdownLabel];
    if (self.displayType == YZNoteAudioDisplayNoteList) {
        self.layer.cornerRadius = 8;
        [self.playImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.offset(0);
            make.left.offset(18);
        }];
        
        [waveImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.offset(0);
            make.left.mas_equalTo(self.playImageView.mas_right).offset(18);
        }];
        
        [self.countdownLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.offset(0);
            make.right.offset(-20);
        }];
        
    }else{
        self.layer.cornerRadius = 4;
        [self.playImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.offset(0);
            make.left.offset(24);
        }];
        
        [waveImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.offset(0);
            make.left.mas_equalTo(self.playImageView.mas_right).offset(18);
        }];
        
        [self.countdownLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.offset(0);
            make.right.offset(-40);
        }];
        
//        UIView *middleLineView = [[UIView alloc] init];
//        middleLineView.backgroundColor = kColorWithHex(0x32BE60);
//        [self addSubview:middleLineView];
//        [middleLineView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerY.offset(0);
//            make.left.mas_equalTo(self.playImageView.mas_right).offset(10);
//            make.size.mas_equalTo(CGSizeMake(139, 1));
//        }];
        
        [self setLayerShadow:kColorWithHexAlpha(0xD9D9D9,0.65) offset:CGSizeMake(0, 5) radius:4];
    }
    @weakify(self)
    [[self rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        if ([[HcdAudioPlayer sharedInstance].audioPlayer isPlaying]) {
            [self stopPlay];
        }else{
            [self startPlay];
        }
    }];
}

- (void)setTotalTimeInterval:(NSInteger)totalTimeInterval{
    if (totalTimeInterval) {
        _totalTimeInterval = totalTimeInterval;
        self.countdownInterval = self.totalTimeInterval;
        self.countdownLabel.text = [NSDate yz_countdownTimeStr:self.totalTimeInterval];
    }
}

- (void)stopPlay{
    self.playImageView.highlighted = NO;
    [self releaseCountdownTimer];
    [[HcdAudioPlayer sharedInstance] manageAudioWithUrlPath:self.audioUrl playOrPause:NO];
}
- (void)startPlay{
    self.playImageView.highlighted = YES;
    [[HcdAudioPlayer sharedInstance] manageAudioWithUrlPath:self.audioUrl playOrPause:YES];
    [self setupCountdownTimer];
}

- (void)setupCountdownTimer {
    [self releaseCountdownTimer];
    self.countdownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateCountdown) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.countdownTimer forMode:NSRunLoopCommonModes];
}

- (void)releaseCountdownTimer {
    [self.countdownTimer invalidate];
}

- (void)updateCountdown{
    --self.countdownInterval;
    if (self.countdownInterval >= 0) {
        self.countdownLabel.text = [NSDate yz_countdownTimeStr:self.countdownInterval];
    }else{
        _countdownInterval = _totalTimeInterval;
        self.playImageView.highlighted = NO;
        self.countdownLabel.text = [NSDate yz_countdownTimeStr:self.totalTimeInterval];
        [self releaseCountdownTimer];
    }
}
- (void)dealloc{
    [self stopPlay];
    [self releaseCountdownTimer];
}

@end
